package au.com.cgu.RescueU;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.util.List;

public class ClaimController {

	public Claim lodge(Claim claim) {
		claim.setClaimNumber("CGU13001234");
		generateHTML(claim);
		return claim;
	}
	
	private void generateHTML(Claim claim) {
		StringBuilder sb = new StringBuilder();
		sb.append("<html>").append("\n");
		sb.append("<header>").append("\n");
		sb.append("<title>").append(claim.getPolicyHolder()).append("</title>").append("\n");
		sb.append("</header>").append("\n");
		sb.append("<body>").append("\n");
		sb.append("<table border=\"1\">").append("<tr>");
		sb.append("<td>").append("Policy Holder").append("</td>");
		sb.append("<td>").append(claim.getPolicyHolder()).append("</td>");
		sb.append("</tr>").append("\n");
		sb.append("<tr>");
		sb.append("<td>").append("Policy Number").append("</td>");
		sb.append("<td>").append(claim.getPolicyNumber()).append("</td>");
		sb.append("</tr>").append("\n");
		sb.append("<tr>");
		sb.append("<td>").append("Claim Number").append("</td>");
		sb.append("<td>").append(claim.getClaimNumber()).append("</td>");
		sb.append("</tr>").append("\n");
		sb.append("<tr>");
		sb.append("<td>").append("What Happened?").append("</td>");
		sb.append("<td>").append(claim.getDescription()).append("</td>");
		sb.append("</tr>").append("\n");
		sb.append("<tr>");
		sb.append("<td>").append("Date/Time of Incident").append("</td>");
		sb.append("<td>").append(claim.getIncidentTime()).append("</td>");
		sb.append("</tr>").append("\n");
		sb.append("<tr>");
		sb.append("<td>").append("Insured at Fault").append("</td>");
		sb.append("<td>").append(claim.getInsuredAtFault()).append("</td>");
		sb.append("</tr>").append("\n");
		sb.append("</table>").append("\n");
		Location loc = claim.getLossLocation();
		if (loc != null) {
			sb.append("<table border=\"1\">");
			sb.append("<tr>");
			sb.append("<td>").append("Address Line 1").append("</td>");
			sb.append("<td>").append(loc.getAddressLine1()).append("</td>");
			sb.append("</tr>").append("\n");
			sb.append("<tr>");
			sb.append("<td>").append("Address Line 2").append("</td>");
			sb.append("<td>").append(loc.getAddressLine2()).append("</td>");
			sb.append("</tr>").append("\n");
			sb.append("<tr>");
			sb.append("<td>").append("Address City").append("</td>");
			sb.append("<td>").append(loc.getSuburb()).append("</td>");
			sb.append("</tr>").append("\n");
			sb.append("<tr>");
			sb.append("<td>").append("Address Post Code").append("</td>");
			sb.append("<td>").append(loc.getPostalCode()).append("</td>");
			sb.append("</tr>").append("\n");
			sb.append("<tr>");
			sb.append("<td>").append("Address State").append("</td>");
			sb.append("<td>").append(loc.getState()).append("</td>");
			sb.append("</tr>").append("\n");
			sb.append("<tr>");
			sb.append("<td>").append("Address Country").append("</td>");
			sb.append("<td>").append(loc.getCountry()).append("</td>");
			sb.append("</tr>").append("\n");
			sb.append("<tr>");
			sb.append("<td>").append("Address Lattitude").append("</td>");
			sb.append("<td>").append(loc.getLatitude()).append("</td>");
			sb.append("</tr>").append("\n");
			sb.append("<tr>");
			sb.append("<td>").append("Address Longitude").append("</td>");
			sb.append("<td>").append(loc.getLongitude()).append("</td>");
			sb.append("</tr>").append("\n");
			sb.append("</table>").append("\n");
		}
		ThirdParty tp = claim.getThirdParty();
		if (tp != null) {
			sb.append("<table border=\"1\">");
			sb.append("<tr>");
			sb.append("<td>").append("Third Party Name").append("</td>");
			sb.append("<td>").append(tp.getName()).append("</td>");
			sb.append("</tr>").append("\n");
			sb.append("<tr>");
			sb.append("<td>").append("Third Party Phone").append("</td>");
			sb.append("<td>").append(tp.getPhoneNumber()).append("</td>");
			sb.append("</tr>").append("\n");
			sb.append("<tr>");
			sb.append("<td>").append("Third Party Email").append("</td>");
			sb.append("<td>").append(tp.getEmailAddress()).append("</td>");
			sb.append("</tr>").append("\n");
			sb.append("<tr>");
			sb.append("<td>").append("Third Party Licence").append("</td>");
			sb.append("<td>").append(tp.getLicenceNumber()).append("</td>");
			sb.append("</tr>").append("\n");
			sb.append("</table>").append("\n");
		}
		Vehicle veh = claim.getVehicle();
		if (veh != null) {
			sb.append("<table border=\"1\">");
			sb.append("<tr>");
			sb.append("<td>").append("Vehicle").append("</td>");
			sb.append("<td>").append(veh.getName()).append("</td>");
			sb.append("</tr>").append("\n");
			sb.append("<tr>");
			sb.append("<td>").append("Vehicle Reg").append("</td>");
			sb.append("<td>").append(veh.getRegistrationNumber()).append("</td>");
			sb.append("</tr>").append("\n");
			sb.append("</table>").append("\n");
		}

		List<Photo> photos1 = claim.getPhotos();
		for (Photo ph : photos1) {
			sb.append("<img width=\"100%\" src=\"").append(ph.getLocation()).append("\">\n");
		}
		sb.append("</body></html>\n");
		
		try {
		BufferedWriter bw = new BufferedWriter(new FileWriter("c:\\tmp\\output.html"));
		bw.write(sb.toString());
		bw.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		try {
			if (claim.getPhotos() != null && claim.getPhotos().size() != 0)
			{
				Photo photo = claim.getPhotos().get(0);
				FileOutputStream imgStream = new FileOutputStream(new File ("c:\\tmp\\" + photo.getPhotoName()));
				imgStream.write(photo.getData());
				imgStream.close();
			}
			
		BufferedWriter bw = new BufferedWriter(new FileWriter("c:\\tmp\\output.html"));
		bw.write(sb.toString());
		bw.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		
	}
}
